# -*- coding: utf-8 -*-
from numpy import *
import operator
import time

def loadDataSet(fileName):
    dataMat = []
    labelMat = []
    with open(fileName) as fr:
        for line in fr.readlines():
            lineArr = line.strip().split()
            dataMat.append([float(lineArr[0]), float(lineArr[1])])
            labelMat.append(float(lineArr[2]))
    return dataMat, labelMat

# The first one, i , is the index of our first alpha,
# and m is the total number of alphas. A value is randomly chosen
# and returned as long as it’s not equal to the input i.
def selectJrand(i, m):
    j = i
    while (j == i):
        j = int(random.uniform(0, m))
    return j

#剪辑大于H或小于L的α值。
#sometime alpha may larger or smaller than H or L,so we have to constrain it
def clipAlpha(aj, H, L):  #alphas[j],H,L
    if aj > H:
        aj = H
    if L > aj:
        aj = L
    return aj


def smoSimple(dataMatIn,classLabels,C,toler,maxIter):
    dataMatrix=mat(dataMatIn)
    labelMat=mat(classLabels).transpose()
    b=0
    m,n=shape(dataMatrix)
    alphas = mat(zeros((m,1)))
    iter=0
    while (iter<maxIter):
        alphaPairsChanged = 0
        for i in range(m):
            fXi = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b  #这里是a 的向量集合 ___g(x)=aj*yj*xj*xi+b dataMatrix[i,:] 代表当前的一条数据
            Ei = fXi - float(labelMat[i])
            if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)): #toler是阈值
                j = selectJrand(i,m) #随便在下标 i，m 取间选一个,除自身之外
                fXj = float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b
                Ej = fXj - float(labelMat[j])
                alphaIold = alphas[i].copy()
                alphaJold = alphas[j].copy()
                if (labelMat[i] != labelMat[j]):
                    L = max(0, alphas[j] - alphas[i])
                    H = min(C, C + alphas[j] - alphas[i])
                else:
                    L = max(0, alphas[j] + alphas[i] - C)
                    H = min(C, alphas[j] + alphas[i])
                if L==H:  continue
                eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T -dataMatrix[i,:]*dataMatrix[i,:].T -dataMatrix[j,:]*dataMatrix[j,:].T
                if eta >= 0:continue
                alphas[j] -= labelMat[j]*(Ei - Ej)/eta
                alphas[j] = clipAlpha(alphas[j],H,L)
                if (abs(alphas[j] - alphaJold) < 0.00001):
                    # print ("j not moving enough")
                    continue
                alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j])
                b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T -labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T
                b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T -labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T
                if (0 < alphas[i]) and (C > alphas[i]): b = b1
                elif (0 < alphas[j]) and (C > alphas[j]): b = b2
                else: b = (b1 + b2)/2.0
                alphaPairsChanged += 1
                # print ("iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged))
        if (alphaPairsChanged == 0): iter += 1
        else: iter = 0
        # print ("iteration number: %d" % iter)
    return b,alphas,labelMat,dataMatrix

def main():
    import numpy as np
    # dataArr,labelArr = loadDataSet('testSet.txt')#特征，目标
    # b,alphas,labelMat,dataMatrix = smoSimple(dataArr,labelArr,2,0.001,40) #依次是目标数据 C 学习率  迭代次数
    # np.save('b.npy',b)
    # np.save('alphas.npy',alphas)
    # np.save('labelMat.npy',labelMat)
    # np.save('dataMatrix.npy',dataMatrix)
    b=np.load('b.npy')
    alphas=np.load('alphas.npy')
    labelMat=np.load('labelMat.npy')
    dataMatrix=np.load('dataMatrix.npy')
    fx = float(multiply(alphas, labelMat).T * (dataMatrix * mat([[100,400 ]]).T)) + b
    res=fx[0][0]
    if res<0:
        return -1
    else:
        return 1
print(main())